
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EBAS_SP_HENT_BOG]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[EBAS_SP_HENT_BOG]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE [DBO].[EBAS_SP_HENT_BOG] 
	@session varchar(255),
	@isbn varchar(255) = NULL,
	@bogpakkeGuid varchar(255) = NULL
WITH ENCRYPTION
AS
	DECLARE @rettighed int,
		@antal int, 
		@guid uniqueidentifier,
		@bogGuid uniqueidentifier
	
	SET NOCOUNT ON
	SET @antal = 0

	CREATE TABLE #temp ( rett int )
	INSERT INTO #temp (rett)
	EXEC EBAS.DBO.EBAS_SP_SIKKERHED_VERIFICER_SESSION @session	

	SET @rettighed = (SELECT IsNull(MAX(rett),0) FROM #temp)
	
	IF 0 < @rettighed 
	BEGIN
		SET @antal = (SELECT COUNT(*) FROM EBAS_BOG)
		
		IF @isbn IS NULL AND @bogpakkeGuid IS NULL
			SELECT 	GUID, TITEL, FORFATTER, FORLAG, ISBN, @antal AS ANTAL
			FROM 	EBAS_BOG
		ELSE IF @bogpakkeGuid IS NOT NULL
		BEGIN
			SET @antal = (	
				SELECT 	COUNT(*)
				FROM 	EBAS_BOG
				WHERE	GUID IN (
					SELECT BOG_GUID
					FROM EBAS_BOGPAKKE_BOG
					WHERE BOGPAKKE_GUID = @bogpakkeGuid
				)
			)

			SELECT 	GUID, TITEL, FORFATTER, FORLAG, ISBN, @antal AS ANTAL
			FROM 	EBAS_BOG
			WHERE	GUID IN (
				SELECT BOG_GUID
				FROM EBAS_BOGPAKKE_BOG
				WHERE BOGPAKKE_GUID = @bogpakkeGuid
			)
		END
		ELSE
			SELECT 	GUID, TITEL, FORFATTER, FORLAG, ISBN, 1 AS ANTAL
			FROM 	EBAS_BOG
			WHERE ISBN = @isbn
	END
	ELSE			
		SELECT 	GUID, TITEL, FORFATTER, FORLAG, ISBN, 0 AS ANTAL
		FROM 	EBAS_BOG
		WHERE	GUID IS NULL
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
